package com.showde.shoppingmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.kim.androidbase.tools.StringUtils;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.SearchResultAdapter;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductSearchLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.domain.SearchResult;
import com.showde.shoppingmall.utils.ActivityDispatch;
import com.showde.shoppingmall.utils.ToastUtils;
import com.showde.shoppingmall.view.ProgressDialog;

public class SearchResultActivity extends BaseActivity {

	private static final int MSG_DISMISS_DIALOG = 0x0001;

	private EditText searchKeyEditText;
	private Button searchButton;
	ProgressDialog dialog;

	private ListView resultListView;

	private List<SearchResult> searchResults = new ArrayList<SearchResult>();

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_DISMISS_DIALOG:
				dismissDialog();
				refreshData();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			search();
			break;

		default:
			break;
		}
	}

	@Override
	protected void initParam() {

	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_searchresult);
		searchKeyEditText = (EditText) findViewById(R.id.edt_searchkey);
		searchButton = (Button) findViewById(R.id.btn_search);
		searchButton.setOnClickListener(this);
		resultListView = (ListView) findViewById(R.id.lst_product);
		resultListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				SearchResult  searchResult = searchResults.get(position);
				if(searchResults == null){
					return ;
				}
				Bundle bundle = new Bundle();
				bundle.putString(ActivityDispatch.PRODUCT_ID,
						searchResult.pID);
				ActivityDispatch.openNewActivity(
						SearchResultActivity.this,
						ProductDetailsActivity.class,
						ActivityDispatch.getActivityGroup(groupTag),
						"", bundle, groupTag);
			}
		});
	}

	private void search() {
		String searchKey = searchKeyEditText.getText().toString();
		if (StringUtils.isEmptyOrNull(searchKey)) {
			ToastUtils.showToast(this, "关键词不能为空...");
			return;
		}
		if (dialog != null) {
			dialog.dismiss();
		}

		dialog = new ProgressDialog(SearchResultActivity.this.getParent(), "正在手机...", R.layout.view_progress);
		dialog.show();
		
		
		ProductSearchLogic.searchProduct(this, /* "0", */searchKey,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						if (data != null && data.data != null) {
							searchResults.clear();
							searchResults
									.addAll((List<SearchResult>) data.data);
						}
						handler.sendEmptyMessage(MSG_DISMISS_DIALOG);
					}

					@Override
					public void onError(String errorMsg) {
						handler.sendEmptyMessage(MSG_DISMISS_DIALOG);
					}
				});
	}

	private void refreshData() {
		if (searchResults.size() > 0) {
			resultListView.setAdapter(new SearchResultAdapter(this,
					searchResults));
		}
	}

	private void dismissDialog() {
		if (dialog != null) {
			dialog.dismiss();
		}
	}

}
